/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

/**
 * 
 */
package hbi.wire.java.host;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.Map;

class MetaValue implements IncomingTag
{

    private final ResolvedType type;

    private final Map<String, Object> map;

    MetaValue(ResolvedType type, Map<String, Object> map)
    {
        this.type = type;
        this.map = map;
    }

    private Method method = null;

    private DataTagList argTags = null;

    public boolean processIncomingData(ByteBuffer buffer) throws IOException
    {
        if (buffer.hasRemaining())
        {
            if (method == null)
            {
                if (buffer.remaining() < 2) return false;
                int methodIndex = buffer.getShort() & 0xFFFF;
                this.method = this.type.getResolvedMethod(methodIndex);
                argTags = new DataTagList(new Class<?>[] { method
                    .getReturnType() });
            }

            assert argTags != null;

            if (argTags.processIncomingData(buffer))
            {
                map.put(this.method.getName(), Array.get(
                    argTags.getTagValues(), 0));
                return true;
            }
        }
        return false;
    }

}
